package com.xiaomili.halfsugar.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xiaomili.halfsugar.CircleImageView;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.adapter.RecyclerViewAdapter.MyHolder;
import com.xiaomili.halfsugar.bean.LatestBean.DataBean.TopicBean;

import java.util.List;

/**
 * Created by Administrator on 2016/7/20.
 */
public class RecyclerViewAdapter extends Adapter<MyHolder> implements OnClickListener {

    private Context mContext;
    private List<TopicBean> mDatas;
    private onItemClickListener mListener;

    public RecyclerViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler_com_article_hotest_and_latest, parent, false);
        return new MyHolder(view, mListener);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        TopicBean topicBean = mDatas.get(position);
        holder.mTvTitle.setText(topicBean.title);
        holder.mTvDesc.setText(topicBean.desc);
        Picasso.with(mContext).load(topicBean.pics.get(0).url).into(holder.mIvUrl);
        Picasso.with(mContext).load(topicBean.user.avatar).into(holder.mCivAvatar);
        holder.mTvNickname.setText(topicBean.user.nickname);
        holder.mTvCreateTimeStr.setText("| " + topicBean.create_time_str);
        holder.mTvViews.setText(topicBean.views);
        holder.mTvComments.setText(topicBean.comments);


    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public void onClick(View v) {

    }

    class MyHolder extends ViewHolder implements OnClickListener {
        private TextView mTvTitle;
        private TextView mTvDesc;
        private ImageView mIvUrl;
        private CircleImageView mCivAvatar;
        private TextView mTvNickname;
        private TextView mTvCreateTimeStr;
        private TextView mTvViews;
        private TextView mTvComments;

        public MyHolder(View itemView, onItemClickListener listener) {
            super(itemView);
            mListener = listener;
            itemView.setOnClickListener(this);
            mTvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            mTvDesc = (TextView) itemView.findViewById(R.id.tv_desc);
            mIvUrl = (ImageView) itemView.findViewById(R.id.iv_url);
            mCivAvatar = (CircleImageView) itemView.findViewById(R.id.civ_avatar);
            mTvNickname = (TextView) itemView.findViewById(R.id.tv_nickname);
            mTvCreateTimeStr = (TextView) itemView.findViewById(R.id.tv_create_time_str);
            mTvViews = (TextView) itemView.findViewById(R.id.tv_views);
            mTvComments = (TextView) itemView.findViewById(R.id.tv_comments);
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onClick(v, getAdapterPosition());
            }
        }
    }

    public void setData(List<TopicBean> datas) {
        mDatas = datas;
        notifyDataSetChanged();
    }

    //暴露出公共的方法设置单项的监听事件
    public void setOnItemClickListener(onItemClickListener listener) {
        this.mListener = listener;
    }

    //定义一个接口用来监听每个单项的点击事件
    public interface onItemClickListener {
        void onClick(View view, int position);
    }
}
